package com.example.booklistapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private List<BookStore> books;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();

        RecyclerView mainRecycleView = findViewById(R.id.recycle_view_books);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mainRecycleView.setLayoutManager(layoutManager);
        mainRecycleView.setAdapter(new MyRecyclerViewAdapter(books));
    }

    public void initData() {
        books = new ArrayList<BookStore>();
        books.add(new BookStore("信息安全数学基础", R.drawable.book_1));
        books.add(new BookStore("软件项目管理案例", R.drawable.book_2));
        books.add(new BookStore("book no name", R.drawable.book_no_name));
    }

    private class MyRecyclerViewAdapter extends RecyclerView.Adapter {
        private List<BookStore> books;

        public MyRecyclerViewAdapter(List<BookStore> books) {
            this.books = books;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.book_holder, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Holder, int position) {

            MyViewHolder holder = (MyViewHolder) Holder;

            holder.getImageView().setImageResource(books.get(position).getPictureId());
            holder.getTextView().setText(books.get(position).getName());
        }

        @Override
        public int getItemCount() {
            return books.size();
        }

        private class MyViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {
            private final ImageView imageView;
            private final TextView textView;

            public MyViewHolder(View itemView) {
                super(itemView);

                this.imageView = itemView.findViewById(R.id.image_view_book);
                this.textView = itemView.findViewById(R.id.text_view_book_name);

                itemView.setOnCreateContextMenuListener(this);
            }

            public ImageView getImageView() {
                return imageView;
            }

            public TextView getTextView() {
                return textView;
            }

            public void onCreateContextMenu(ContextMenu contextmenu, View view, ContextMenu.ContextMenuInfo contextmenuInfo) {

                int position = getAdapterPosition();

                MenuItem menuItemAdd = contextmenu.add(Menu.NONE, 1, 1, "Add" + position);
                MenuItem menuItemEdit = contextmenu.add(Menu.NONE, 2, 2, "Edit" + position);
                MenuItem menuItemDelete = contextmenu.add(Menu.NONE, 3, 3, "Delete" + position);

                menuItemAdd.setOnMenuItemClickListener(this);
                menuItemEdit.setOnMenuItemClickListener(this);
                menuItemDelete.setOnMenuItemClickListener(this);


            }

            public boolean onMenuItemClick(MenuItem menuItem) {
                int position = getAdapterPosition();
                switch (menuItem.getItemId()) {
                    case 1:
                        View dialogueView = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialogue_input_books, null);
                        AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(MainActivity.this);
                        alerDialogBuilder.setView(dialogueView);
                        alerDialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                EditText editName = dialogueView.findViewById(R.id.edit_text_name);
                                books.add(position, new BookStore(editName.getText().toString(), R.drawable.book_1));
                                MyRecyclerViewAdapter.this.notifyItemInserted(position);
                            }
                        });
                        alerDialogBuilder.setCancelable(false).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });
                        alerDialogBuilder.create().show();
                        break;
                    case 2:
                        books.get(position).setName("测试修改");
                        MyRecyclerViewAdapter.this.notifyItemChanged(position);
                        break;
                    case 3:
                        books.remove(position);
                        MyRecyclerViewAdapter.this.notifyItemRemoved(position);
                        break;
                }
                Toast.makeText(MainActivity.this, "点击了" + menuItem.getItemId(), Toast.LENGTH_LONG).show();
                return false;
            }
        }
    }
}